package org.iteam.xlz.core.service.ad;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.iteam.core.utils.SqlBuilder;
import org.iteam.db.BaseService;
import org.iteam.xlz.core.entity.ad.AdPosVo;

public class AdPosService extends BaseService<AdPosVo>{
	static AdPosService instance;
	
	private AdPosService(){
	}
	
	public static AdPosService getInstance(){
		if(null==instance){
			instance=new AdPosService();
		}
		return instance;
	}
	
	public long counts(){
		try {
			return super.counts("select count(1) from adpos where 1=1");
		} catch (SQLException e) {
			e.printStackTrace();
			return 0;
		}
	}
	
	public List<AdPosVo> query(long page,long rows){
		try {
			return super.query("select ad.*,at.name posTypeName from adpos ad left join adtype at on ad.posTypeId = at.id where 1=1 limit ?,?",(page-1)*rows,rows);
		} catch (SQLException e) {
			e.printStackTrace();
			return new ArrayList<AdPosVo>();
		}
	}
	
	
	public boolean create(AdPosVo model){
		model.setCreateTime(new Date());
		SqlBuilder builder=SqlBuilder.createInsert("adpos");
		builder.addField("posName",model.getPosName());
		builder.addField("posTypeId",model.getPosTypeId());
		builder.addField("width",model.getWidth());
		builder.addField("height",model.getHeight());
		builder.addField("isEnable",model.getIsEnable());
		builder.addField("createTime",model.getCreateTime());
		builder.addField("creator",model.getCreator());
		try {
			return super.update(builder.getSql(),builder.getParams());
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}
	}
	
	public boolean update(AdPosVo model){
		SqlBuilder builder=SqlBuilder.createUpdate("adpos");
		builder.addField("posName",model.getPosName());
		builder.addField("posTypeId",model.getPosTypeId());
		builder.addField("posTypeName",model.getPosTypeName());
		builder.addField("width",model.getWidth());
		builder.addField("height",model.getHeight());
		builder.addField("isEnable",model.getIsEnable());
		builder.addWhere("id",model.getId());
		try {
			return super.update(builder.getSql(),builder.getParams());
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}
	}

	
	public AdPosVo findById(String id){
		try {
			return super.show("select * from adpos where 1=1 and id=?",id);
		} catch (SQLException e) {
			e.printStackTrace();
			return new AdPosVo();
		}
	}
	
	public boolean delete(String id){
		try {
			return super.updates("delete from adpos where id=?",id)>0;
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}
	}	
	
	public List<AdPosVo> getPosType(){
		try {
			return super.query("select id posTypeId,name posTypeName from adtype");
		} catch (SQLException e) {
			e.printStackTrace();
			return new ArrayList<AdPosVo>();
		}
	}
	
	public List<AdPosVo> getAdPosInfoList(){
		try {
			return super.query("select * from adpos");
		} catch (SQLException e) {
			e.printStackTrace();
			return new ArrayList<AdPosVo>();
		}
	}
}
